<template>
	<view class="chat-page">
		<!-- 搜索栏 -->
		<view class="search-bar">
			<view class="search-icon">
				<image src="/static/fang_da_jing.svg" mode="aspectFit"></image>
			</view>
			<view class="add-icon">
				<image src="/static/jia_hao.svg" mode="aspectFit"></image>
			</view>
		</view>
		
		<!-- 聊天列表 -->
		<scroll-view class="chat-list" scroll-y="true">
			<view 
				class="chat-item" 
				v-for="(item, index) in chatList" 
				:key="index"
				@click="openChat(item)"
			>
				<view class="avatar">
					<view class="avatar-default" v-if="!item.avatar">{{ item.name.charAt(0) }}</view>
					<image v-else :src="item.avatar" mode="aspectFill"></image>
				</view>
				<view class="chat-content">
					<view class="chat-info">
						<text class="name">{{ item.name }}</text>
						<text class="time">{{ item.time }}</text>
					</view>
					<view class="message-preview">
						<text class="message">{{ item.lastMessage }}</text>
						<view class="badge" v-if="item.unread > 0">{{ item.unread }}</view>
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'

// 缓存key
const CHAT_LIST_CACHE_KEY = 'chat_list'

// 从缓存加载聊天列表
const loadChatListFromCache = () => {
	try {
		const cachedChatList = uni.getStorageSync(CHAT_LIST_CACHE_KEY)
		if (cachedChatList && Array.isArray(cachedChatList) && cachedChatList.length > 0) {
			return cachedChatList
		}
	} catch (error) {
		console.error('加载聊天列表失败:', error)
	}
	// 返回默认数据
	return [
		{
			id: 1,
			name: '我们51',
			avatar: '/static/avatar/robot.jpg',
			lastMessage: '你好！我是我们51，有什么可以帮助你的吗？',
			time: '刚刚',
			unread: 0
		}
	]
}

// 保存聊天列表到缓存
const saveChatListToCache = (chatList) => {
	try {
		uni.setStorageSync(CHAT_LIST_CACHE_KEY, chatList)
	} catch (error) {
		console.error('保存聊天列表失败:', error)
	}
}

// 聊天列表数据
const chatList = ref(loadChatListFromCache())

// 打开聊天
const openChat = (chatItem) => {
	uni.navigateTo({
		url: `/pages/chat-detail/index?id=${chatItem.id}&name=${chatItem.name}`
	})
}
</script>

<style scoped>
.chat-page {
	background-color: #f7f7f7;
	height: 100vh;
}

.search-bar {
	display: flex;
	justify-content: space-between;
	align-items: center;
	padding: 8px 16px;
	background-color: #f7f7f7;
	border-bottom: 1px solid #e5e5e5;
}

.search-icon, .add-icon {
	width: 36px;
	height: 36px;
	display: flex;
	align-items: center;
	justify-content: center;
	border-radius: 4px;
	background-color: rgba(0, 0, 0, 0);
}

.search-icon:active, .add-icon:active {
	background-color: rgba(0, 0, 0, 0.1);
}

.search-icon image, .add-icon image {
	width: 22px;
	height: 22px;
	display: block;
	background-color: transparent;
}

.chat-list {
	flex: 1;
	background-color: #fff;
}

.chat-item {
	display: flex;
	padding: 12px 16px;
	border-bottom: 1px solid #f0f0f0;
	background-color: #fff;
}

.chat-item:active {
	background-color: #f5f5f5;
}

.avatar {
	width: 50px;
	height: 50px;
	margin-right: 12px;
	border-radius: 6px;
	overflow: hidden;
	flex-shrink: 0;
	position: relative;
	box-sizing: border-box;
	display: flex;
	align-items: center;
	justify-content: center;
}

.avatar image {
	width: 50px;
	height: 50px;
	display: block;
	border-radius: 6px;
	background-color: #e5e5e5;
	box-sizing: border-box;
	/* #ifndef H5 */
	position: absolute;
	top: 0;
	left: 0;
	/* #endif */
}

.avatar-default {
	width: 100%;
	height: 100%;
	background: linear-gradient(45deg, #007aff, #4cd964);
	display: flex;
	align-items: center;
	justify-content: center;
	color: white;
	font-weight: bold;
	font-size: 18px;
	border-radius: 6px;
}

.chat-content {
	flex: 1;
	display: flex;
	flex-direction: column;
	justify-content: space-between;
	min-width: 0; /* 确保flex子元素可以正确收缩 */
	overflow: hidden;
}

.chat-info {
	display: flex;
	justify-content: space-between;
	align-items: center;
	margin-bottom: 6px;
	min-width: 0; /* 确保flex子元素可以正确收缩 */
}

.name {
	font-size: 16px;
	color: #333;
	font-weight: 400;
	flex: 1;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
	margin-right: 8px; /* 与时间保持间距 */
}

.time {
	font-size: 12px;
	color: #999;
	flex-shrink: 0; /* 防止时间被压缩 */
	white-space: nowrap;
}

.message-preview {
	display: flex;
	justify-content: space-between;
	align-items: center;
	min-width: 0; /* 确保flex子元素可以正确收缩 */
}

.message {
	font-size: 14px;
	color: #999;
	flex: 1;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
	min-width: 0; /* 确保flex子元素可以正确收缩 */
	margin-right: 8px; /* 与徽略数保持间距 */
}

.badge {
	background-color: #ff4d4f;
	color: #fff;
	font-size: 12px;
	padding: 2px 6px;
	border-radius: 10px;
	min-width: 16px;
	text-align: center;
	margin-left: 8px;
	flex-shrink: 0; /* 防止徽略数被压缩 */
	white-space: nowrap;
}
</style>